//房源panal
Ext.define('customer.subscribe.view.estatePanel',{
	extend : 'Ext.form.Panel',
	initComponent:function(){
		var me=this;
		var row_1={
				id:"isPaydepositTrue",
				fieldDefaults: {
			            labelAlign: 'top',
			            msgTarget: 'side'
			    },
			    defaults: {
			            border: false,
			            xtype: 'panel',
			            flex: 1,
			            layout: 'anchor'
			    },
			    border: false,
				layout: 'hbox',
				items: [{
		            items: [{
		            	xtype:'container',
		            	layout:'hbox',
		            	border:true,
		            	items:[{
		            		xtype:'displayfield',
			                fieldLabel: '房号',
			                name: 'combinedName'
			                }]
		                
		            }, {
		                xtype:'displayfield',
		                fieldLabel: '建筑面积(㎡)',
		                anchor: '80%',
		                name: 'buildingArea'
		            }, {
		                xtype:'displayfield',
		                fieldLabel: '分摊面积(㎡)',
		                anchor: '80%',
		                name: 'sharingArea'
		            }, {
		                xtype:'displayfield',
		                fieldLabel: '折前单价(元)',
		                anchor: '80%',
		                name: 'unitPrice',
		                readOnly:true
		            }]
		        }, {
		            items: [{
		                xtype:'displayfield',
		                fieldLabel: '房源状态',
		                anchor: '80%',
		                name: 'roomStatusName'
		            },{
		                xtype:'displayfield',
		                fieldLabel: '套内面积(㎡)',
		                anchor: '80%',
		                name: 'innerArea'
		            },{
		                xtype:'displayfield',
		                fieldLabel: '计价面积(㎡)',
		                anchor: '80%',
		                name: 'calculateArea'
		            },{
		                xtype:'displayfield',
		                fieldLabel: '折前总价(元)',
		                anchor: '80%',
		                name: 'totalPrice'
		            }]
		        }]
	    };
		var items=[row_1];
		var initConfigg = {
				id:'estatePanel',
				title:"房产信息",
				bodyStyle: 'padding:5px 5px 0',
		        minHeight: 200,
		        fieldDefaults: {
		            labelAlign: 'left',
		            msgTarget: 'side',
		            margin: 5
		        },
		        items:items
		};
		Ext.apply(this,initConfigg);
		this.callParent(arguments);
	}
});

Ext.define('customer.subscribe.view.customerPanel',{
	extend : 'Ext.form.Panel',
	initComponent:function(){
		var me=this;
		var row_1={
				fieldDefaults: {
			            labelAlign: 'top',
			            msgTarget: 'side'
			    },
			    border: false,
			    defaults: {
			            border: false,
			            xtype: 'panel',
			            flex: 1,
			            layout: 'anchor'
			    },
				layout: 'hbox',
				items: [{
		            items: [{
		            	xtype:'container',
		            	layout:'hbox',
		            	anchor: '80%',
		            	items:[{
		            		xtype:'displayfield',
			                fieldLabel: '客户姓名',
			                anchor: '80%',
			                name: 'customerName'
			                }]
		                
		            }, {
		                xtype:'displayfield',
		                fieldLabel: '证件类型',
		                anchor: '80%',
		                name: 'peDocumentTypeName'
		            }, {
		                xtype:'displayfield',
		                fieldLabel: '证件号码',
		                anchor: '80%',
		                name: 'peDocumentNumber'
		            }, {
		                xtype:'displayfield',
		                fieldLabel: '联系电话',
		                anchor: '80%',
		                name: 'customerPhone'
		            }]
		        }, {
		            items: [{
		                xtype:'displayfield',
		                fieldLabel: '邮寄地址',
		                anchor: '80%',
		                name: 'peContactAddress'
		            },{
		                xtype:'displayfield',
		                fieldLabel: '邮编',
		                anchor: '80%',
		                name: 'pePostcode'
		            },{
		                xtype:'displayfield',
		                fieldLabel: '客户来源',
		                anchor: '80%',
		                name: 'customerOriginName'
		            }]
		        }]
	    };
		var documentTypeStore = Ext.create('Ext.data.Store', {
		    fields: ['value', 'name'],
		    data :documentType
		});
		var gridStore = Ext.create('Ext.data.Store', {
			id:'familyMemberGridStore',
		    fields: ["memberName","documentType","documentNumber","mobile"],
		    //是否在服务端排序 （true的话，在客户端就不能排序）
		    remoteSort: false,
		    proxy: {
		        type: 'ajax',
		        url: contextPath+'/customer/familyMember/findList',
		        reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
		            type: 'json', //返回数据类型为json格式
		            root: 'obj'
		        },
		        actionMethods: {read: 'POST'},
	            extraParams:{
	            	buyId:subscribeId,
	            	memberType:'3'
	            }
		    },
		    autoLoad:true
		});
		var columns = [{
			text : "姓名",
			dataIndex : 'memberName',
			width:'25%'
		}, {
			text : "证件类型",
			dataIndex : 'documentType',
			width:'25%',
            renderer: function(value,metadata,record){  
                var index = documentTypeStore.find('value',value);  
                if(index!=-1){  
                    return documentTypeStore.getAt(index).data.name;  
                }  
                return value;  
            }
		}, {
			text : "证件号码",
			dataIndex : 'documentNumber',
			width:'25%'
		},{
			text : "手机号码",
			dataIndex : 'mobile',
			width:'20%'
		}];
		var row_2=Ext.create('Ext.grid.Panel', {
			id:'familyMemberGridPanel',
			region : 'south',
			title:"共同购房者",
			minHeight:200,
			columnLines: true,
	        viewConfig: {
	            stripeRows: true
	        },
			store : gridStore,
			columns : columns
		});
		var items=[row_1,row_2];
		var initConfigg = {
				id:'customerPanel',
		        title:"客户信息",
		        minHeight:320,
		        fieldDefaults: {
		            labelAlign: 'left',
		            msgTarget: 'side',
		            margin: 5
		        },
		        items:items
		};
		Ext.apply(this,initConfigg);
		this.callParent(arguments);
	}
});
Ext.define('customer.subscribe.view.subscribePanel',{
	extend : 'Ext.form.Panel',
	initComponent:function(){
		var me=this;
		var row_1={
				fieldDefaults: {
			            labelAlign: 'left',
			            msgTarget: 'side'
			    },
			    border: false,
			    defaults: {
			            border: false,
			            xtype: 'panel',
			            flex: 1,
			            layout: 'anchor'
			    },
				layout: 'hbox',
				items: [{
		            items: [{
		                xtype:'displayfield',
		                fieldLabel: '折前单价(元)',
		                name:'conversionPrice'
		            }, {
		                xtype:'displayfield',
		                fieldLabel: '认购编号',
		                name:'subscribeCode'
		            }, {
		                xtype:'displayfield',
		                fieldLabel: '签约单价(元)',
		                name: 'subscribePrice'
		            }, {
		                xtype:'displayfield',
		                fieldLabel: '业务状态',
		                name:'businessStatus',
		                renderer : function(val) {
		                	if (val == 1) {
		                        return '认购';
		                    } else if (val == 3) {
		                        return '已换房';
		                    }else if (val == 2) {
		                        return '认购转签约';
		                    }else if (val == 4) {
		                        return '已更名';
		                    }else if (val == 5) {
		                        return '退认购';
		                    }
		                    return val;
		                }
		            }]
		        }, {
		            items: [{
		                xtype:'displayfield',
		                fieldLabel: '折前总价(元)',
		                name: 'lumpsumPrice'
		            },{
		                xtype:'displayfield',
		                fieldLabel: '认购转签约日期',
		                name:'conversionSignDate',
		                renderer:function(val){
		                	if(val==null||val==""){
		                		return "";
		                	}
		                	return Ext.Date.format(new Date(val),'Y-m-d');
		                }
		            },{
		            	xtype: 'displayfield',
		                fieldLabel: '合同金额(元)',
		                name:'subscribeAmount',
		                readOnly:true,
		            }]
		        }]
	    };
		var row_2={
				fieldDefaults: {
		            labelAlign: 'left',
		            msgTarget: 'side'
		        },
		        collapsible: true,
		        //collapsed: true,
				xtype: 'fieldset',
                title:'折扣信息',
                //layout: 'anchor',
                defaultType: 'displayfield',
                items:[
                {
                    	xtype: 'displayfield',
                    	fieldLabel: '折后总价(元)',
                        name: 'discountAfterPrice',
                        id:'discountAfterPrice'
                	
                },{
                	xtype: 'displayfield',
                	fieldLabel: '折扣金额(元)',
                    name: 'discountAmount'
                },{
	            	xtype:'container',
	            	layout:'hbox',
	            	anchor: '80%',
	            	items:[{
	            		xtype: 'displayfield',
		                fieldLabel:'折扣比例(%)',
		                name:'discountRate'
	            	}]
	                
	            },{
                	xtype: 'displayfield',
                	fieldLabel: '折扣说明',
                	anchor: '50%',
                    name: 'discountNote'
                }]
	    };
		var dealCategoryStore = Ext.create('Ext.data.Store', {
		    fields: ['value', 'text'],
		    data : [
                {"value":"1", "text":"推荐成交"},
		        {"value":"2", "text":"单独成交"},
		        {"value":"3", "text":"共同成交"}
		    ]
		});
		var items=[row_1,row_2,,{
        	xtype: 'displayfield',
            fieldLabel: '应收认购金(元)',
            name:'depositReceivable'
        },{
        	xtype: 'displayfield',
            fieldLabel: '已收认购金(元)',
            name:'alsubscribeAmount'
        },{
         	xtype: 'displayfield',
         	hidden:payDepositOff,
            fieldLabel: '已收认筹金(元)',
            name:'receivedPayAmount'
        },{
        	xtype: 'displayfield',
            fieldLabel: '认购日期',
            name:'subscribeDate',
            renderer:function(val){
            	if(val==null||val==""){
            		return "";
            	}
            	return Ext.Date.format(new Date(val),'Y-m-d');
            }
        },{
        	xtype: 'displayfield',
            fieldLabel: '成交类别',
            name:'dealCategory',
            renderer:function(value){
            	if(value=="1"){
        			Ext.getCmp("referenceName").show();
        			//Ext.getCmp("referenceProportion").show();
            		return "推荐成交";
            	}else if(value=="2"){
            		return "单独成交";
            	}else if(value=="3"){
            		return "共同成交";
            	}
            	return "";
            }
        },
            		{xtype:"displayfield", name:"referenceId", hidden:true},
            		{xtype:"displayfield", id:'referenceName', fieldLabel:'推荐人姓名', hidden:true, name:"referenceName"},
            		{xtype:"displayfield", id:'referenceProportion', fieldLabel:'推荐人比例', hidden:true, name:"referenceProportion",maxLength:3,regex:/^[1-9]\d*$/}
        ,{
        	xtype: 'displayfield',
            fieldLabel: '经纪人',
            name:'currentUserName',
            value:userName
        },{
        	xtype: 'displayfield',
            fieldLabel: '备注',
            anchor: '50%',
            name:'remark'
        }];
		var initConfigg = {
				id:"subscribeInfoForm",
		        title:"认购信息",
		        minHeight:200,
		        fieldDefaults: {
		            labelAlign: 'left',
		            msgTarget: 'side',
		            margin: 5
		        },
		        items:items
		};
		Ext.apply(this,initConfigg);
		this.callParent(arguments);
	}
});

function vaildResult(url){
	   Ext.Ajax.request({
   	    url: contextPath+'/marketing/room/vaildNeedReceive',
   	    params: {
   	    	subId: subscribeId
   	    },
   	    method: 'POST',
   	    success: function(response){
   	        var text = Ext.decode(response.responseText);
   	        if(text.result=="1"){
   	        	Ext.Msg.alert('提示', text.msg);
   	        }else if(text.result=="0"){
   	          var tabKey = "sub_to_sig_"+subscribeId;
   	          business(subscribeId,tabKey,"认购转签约",url);
   	        }
   	    }
   	});
}

function business(id,tabKey,title,url){
	   Ext.Ajax.request({
	   	    url: contextPath+'/marketing/businessAbnorma/getBuinessAbnormaByBuinessId',
	   	    params: {
	   	    	buinessId: id
	   	    },
	   	    method: 'POST',
	   	    success: function(response){
	   	        var text = Ext.decode(response.responseText);
	   	        if(text.res=="1"){
	   	        	parent.openMenu("0",tabKey,"",title,url);	  
	   	        }else if(text.res=="0"){
	   	        	Ext.Msg.alert('提示', text.resMsg);
	   	        }
	   	    }
	   	});
}
Ext.define('customer.subscribe.viewPanel',{
	extend : 'Ext.form.Panel',
	initComponent:function(){
		var estatePanel= Ext.create("customer.subscribe.view.estatePanel");
		var customerPanel= Ext.create("customer.subscribe.view.customerPanel");
		var payDepositPanel= Ext.create("customer.subscribe.view.subscribePanel");
		//审批历史
		 var selectWindow=Ext.create('component.proces.history',{
			 processInstanceId: procInstId,
			 contextPath:contextPath
	 	 });
		 //未提交状态，无审核记录，不显示
		 if("0" == auditStatus){
			 selectWindow = null;
		 }
		 
		 var fileAttachment = Ext.create('component.file.attachment',{
		    objectId: subscribeId,
		    imageType: "2",
		    contextPath:contextPath
		});
		 
		 var busWindow;
		if(busProcInstId!=null&&busProcInstId!=""){
			busWindow=Ext.create('component.proces.approval',{
				 processInstanceId: busProcInstId
		 	 });
		}
		var defaultConfigg = {
				buttonAlign:'center',
				defaults: {  
		            split: true,    //有分割线  
		            collapsible: true,  //可以折叠
		        },
		        renderTo:Ext.getBody(),
				items:[estatePanel,customerPanel,payDepositPanel,selectWindow,busWindow, fileAttachment],
				tbar:[{hidden:marketingSubscribeReceivables,xtype: "button", text: "收款",iconCls:'icon-accept',
				    handler:function(){
				      var tabKey = "sub_pay"+subscribeId;
				      var url = "/marketing/proceeds/subscribe?id="+subscribeId+"&1=1";
		    	      business(subscribeId,tabKey,"认购收款",url);
			        }
				},{hidden:marketingSignedAdd,xtype: "button", text: "签约",iconCls:'icon-accept', id:"qianYue",
					     handler:function(){
						    var baseUrl = returnUrl.replace("/", ",").replace("?", "(").replace("=", ")");
					    	while(baseUrl.indexOf('/') >= 0)
					    		baseUrl = baseUrl.replace("/", ",");
					    	while(baseUrl.indexOf('?') >= 0)
					    		baseUrl = baseUrl.replace("?", "(");
					    	while(baseUrl.indexOf('=') >= 0)
					    		baseUrl = baseUrl.replace("=", ")");
							var url='/customer/signed/toAdd?projectId='+projectId+'&subscribeId='+subscribeId+'&returnUrl='+baseUrl;
							vaildResult(url)
					    	
						 }
				 },{hidden:marketingSubscribeChangeHouse,xtype: "button", text: "换房",iconCls:'icon-accept',
				     handler:function(){
				    	 
				    	 Ext.Ajax.request({
			                   url: contextPath+'/marketing/businessAbnorma/isChangeHouse',
			                   params: {
			                	   abnormaId: subscribeId
			                   },
			                   method: 'POST',
			                   success: function(response){
			                       var text = Ext.decode(response.responseText);
			                       if(text.res=="0"){
			                       	 Ext.MessageBox.alert('提示', text.resMsg);
			                       }else if(text.res=="10000"){
				     	       		 Ext.MessageBox.alert("系统提示","系统异常!");
				     	       	   }else if(text.res=="1"){
				     	       		  var tabKey = "abs_"+subscribeId;
								      var url = "/marketing/businessAbnorma/abnormaBusiness?id="+subscribeId+"&1=1";
						    	      business(subscribeId,tabKey,"认购换房",url);
				     	       	   }
			                   }
			            });
							
						 }
				 },{hidden:tuiSubscribe,xtype: "button", text: "退认购",iconCls:'icon-exclamation',
				     handler:function(){
				    	 	var tabKey = "ret_"+subscribeId;
				    	 	var url = "/customer/subscribe/toRetreat?subscribeId="+subscribeId+"&1=1";
				    	 	business(subscribeId,tabKey,"退认购",url);
					 }
				 },{hidden:yanSigned,xtype: "button", text: "延期签约",iconCls:'icon-padlock',
				     handler:function(){
				    	Ext.Ajax.request({
			                   url: contextPath+'/customer/subscribe/toDelaySignedIS',
			                   params: {
			                	   subscribeId: subscribeId
			                   },
			                   method: 'POST',
			                   success: function(response){
			                       var text = Ext.decode(response.responseText);
			                       //res: 0,失败(基本为异常) 1,成功
			                       //resMsg: 0(正常发起), 1(存在审核中内容,不允许发起), 2(存在通过的延期,提醒后发起)
			                       if(text.res=="0"){
			                    	   Ext.MessageBox.alert("系统提示",text.resMsg);
			                    	   return;
			                       }else if(text.res=="1"){
			                    	   if(text.resMsg == "1"){
			                    		   Ext.MessageBox.alert('提示', '已发起延期签约申请，请等待审核。您可在【待办事项-我的发起】中查看审批进度。');
			                    		   return;
			                    	   }else if(text.resMsg == "2"){
			                    		   var obj = text.obj;
			                    		   Ext.MessageBox.confirm(
		                    				   "系统提示",
		                    				   "<h2>于" + obj + ",审批过同类流程。</h2><div style='margin-left: 20%;'><h3 style='color: red;'>是否继续申请?</h3></div>",
		                    				    function(btn){
			                    			   		if("yes" == btn){
			                    			   			var tabKey = "del_"+subscribeId;
								     	       	  		var url = "/customer/subscribe/toDelaySigned?subscribeId="+subscribeId+"&1=1";
								     	       	  		business(subscribeId,tabKey,"延期签约申请",url);
			                    			   		}
                    		   					},
                		   						this
                		   					);
			                    	   }else{
			                    		   	var tabKey = "del_"+subscribeId;
					     	       	  		var url = "/customer/subscribe/toDelaySigned?subscribeId="+subscribeId+"&1=1";
					     	       	  		business(subscribeId,tabKey,"延期签约申请",url);
			                    	   }
				     	       	   }else{
					     	       	  var tabKey = "del_"+subscribeId;
					     	       	  var url = "/customer/subscribe/toDelaySigned?subscribeId="+subscribeId+"&1=1";
					     	       	  business(subscribeId,tabKey,"延期签约申请",url);
				     	       	   }
			                   }
			            });
					 }

				 },{hidden:marketingSubscribeShou,xtype: "button", text: "收款明细",iconCls:'icon-accept',
				     handler:function(){
				    	 var tabKey = "have_"+subscribeId;
		     	       	 var url = "/marketing/pin/haveReceive?receivablesType=1&relationId="+subscribeId+"&returnBackParam="+returnBackParam;
		     	       	 parent.openMenu("0",tabKey,"","收款明细",url);	  	
					 }
				 },{hidden:marketingSubscribeHuan,xtype: "button", text: "换房信息",iconCls:'icon-accept',
				     handler:function(){
					    	 var tabKey = "busDetail_"+oldSubId;
			     	       	 var url = "/marketing/businessAbnorma/businessDetail?id="+oldSubId+"&1=1";
			     	       	 parent.openMenu("0",tabKey,"","换房信息",url);	  	
					 }
				 },{hidden:marketingSubscribeTui,xtype: "button", text: "退认购信息",iconCls:'icon-accept',
				     handler:function(){
					    	var tabKey = "retDetail_"+businessAbnormaId;
			     	       	var url = "/customer/subscribe/toRetreatView?id="+businessAbnormaId+"&1=1";
			     	       	parent.openMenu("0",tabKey,"","退认购信息",url);	  	
					 }
				 },{hidden:marketingSubscribeChangeName,xtype: "button", text: "更名", iconCls:'icon-user',
						handler:function(){
							 var tabKey = "cgn_"+subscribeId;
			     	       	 var url = "/customer/subscribe/toChangeName?subscribeId="+subscribeId+"&1=1";
			     	       	 parent.openMenu("0",tabKey,"","认购更名",url);	  	
				        }
					}]
		};
		Ext.apply(this,defaultConfigg);
		this.callParent(arguments);
	},
	buttons:[
	         {
	        	 id:"retrunBtn",
	        	 xtype: "button",
	        	 text: "返回",
	        	 handler: function(){
	        		 parent.TabControlRemove(subscribeId);
	        	 }
	         }
	]
});


